package org.lzfto.flink.demo.system.config;

import java.io.IOException;

import org.lzfto.flink.demo.common.data.PageData;
import org.lzfto.flink.demo.config.AppConfiguration;
import org.lzfto.flink.demo.config.AppInfo;
import org.lzfto.flink.demo.utils.SecurityUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Lazy
@Service
public class SysConfigService {
    private final SysConfigRepository sysConfigRepository;

    public SysConfigService(SysConfigRepository sysConfigRepository) {
        this.sysConfigRepository = sysConfigRepository;
    }

    public String getConfigValue(String configKey) {
        return sysConfigRepository.findByConfigKey(configKey);
    }

    public SysConfig selectById(int id) {
        return sysConfigRepository.selectById(id);
    }

    public PageData<SysConfig> selectAll(int pageNo,int pageSize,SysConfig condition) {
        return sysConfigRepository.selectByPage(pageNo,pageSize,condition);
    }

    public SysConfig insert(SysConfig sysConfig) {
        return sysConfigRepository.insert(sysConfig);
    }

    public void update(SysConfig sysConfig) {
        sysConfigRepository.update(sysConfig);
    }

    public void deleteById(int id) {
        sysConfigRepository.deleteById(id);
    }

    /**
     * 从account.txt读取加密内容并解密为Account对象
     * @param password 密码
     * @return 解密后的Account对象
     * @throws IOException 读取文件失败时抛出
     */
    public Account getAccount(String password) throws IOException {
       return SecurityUtil.getAccount(password);
    }
    
    public AppInfo getAppInfo() {
    	AppInfo appInfo=new AppInfo();
    	AppConfiguration conf=AppConfiguration.getConf();
    	appInfo.setEnv(conf.getEnv());
    	appInfo.setArtemisHost(conf.getArtemisHost());
    	appInfo.setArtemisUserName(conf.getArtemisUserName());
    	appInfo.setMqttQos(conf.getAppProp().getMqttQos());
    	appInfo.setDbUrl(conf.getAppProp().getDatasource().getUrl());
    	appInfo.setDbUserName(conf.getDbUserName());
    	appInfo.setFlinkAddress(conf.getAppProp().getFlinkAddress());
    	appInfo.setUploadDirectory(conf.getAppProp().getUploadDirectory());
    	return appInfo;
    }
}
